<!-- components/AssignmentDetailDialog.vue -->
<template>
  <el-dialog
    v-model="dialogVisible"
    title="作业详情"
    width="60%"
    :before-close="handleClose"
    class="detail-dialog"
  >
    <div v-if="assignment" class="assignment-detail">
      <div class="header-section">
        <h2>{{ assignment.title }}</h2>
        <div class="status-badge" :class="getStatusClass()">
          {{ getStatusText() }}
        </div>
      </div>

      <el-divider />

      <div class="info-grid">
        <div class="info-item">
          <div class="info-label">
            <el-icon><Calendar /></el-icon>
            提交截止日期
          </div>
          <div class="info-value">{{ formatDate(assignment.due_date) }}</div>
        </div>

        <div class="info-item">
          <div class="info-label">
            <el-icon><Timer /></el-icon>
            互评截止日期
          </div>
          <div class="info-value">{{ formatDate(assignment.peer_review_due_date) }}</div>
        </div>

        <div class="info-item">
          <div class="info-label">
            <el-icon><Star /></el-icon>
            满分
          </div>
          <div class="info-value highlight">{{ assignment.score }}分</div>
        </div>

        <div class="info-item">
  <div class="info-label">
    <el-icon><Document /></el-icon>
    提交情况
  </div>
  <div class="info-value submission-info">
    <el-progress
      :percentage="getSubmissionPercentage()"
      :stroke-width="10"
      status="success"
    />
    <span class="submission-count">
      {{ format() }}
    </span>
  </div>
</div>
      </div>

      <div class="description-section">
        <h3>作业描述</h3>
        <div class="description-content">{{ assignment.description }}</div>
      </div>

      <div v-if="hasAttachments" class="attachments-section">
        <h3>附件列表</h3>
        <el-card shadow="hover" class="attachment-list">
          <div v-for="attachment in assignment.attachments"
               :key="attachment.id"
               class="attachment-item">
            <el-link
              :href="attachment.file_path + '?download'"
              :download="getFileName(attachment.file_path)"
              type="primary">
              <el-icon><Document /></el-icon>
              {{ getFileName(attachment.file_path) }}
              <span class="file-size" v-if="attachment.size">
                {{ formatFileSize(attachment.size) }}
              </span>
            </el-link>
          </div>
        </el-card>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { computed } from 'vue'
import { Calendar, Timer, Star, Document } from '@element-plus/icons-vue'

const props = defineProps({
  visible: {
    type: Boolean,
    required: true
  },
  assignment: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['update:visible', 'close'])

const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
})

const hasAttachments = computed(() => {
  return props.assignment &&
         props.assignment.attachments &&
         props.assignment.attachments.length > 0
})

const formatDate = (dateString) => {
  if (!dateString) return '未设置'
  const date = new Date(dateString)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')

  return `${year}-${month}-${day} ${hours}:${minutes}`
}

const getFileName = (path) => {
  if (!path) return ''
  return path.split('/').pop()
}

const formatFileSize = (bytes) => {
  if (!bytes) return ''
  const units = ['B', 'KB', 'MB', 'GB']
  let size = bytes
  let unitIndex = 0
  while (size >= 1024 && unitIndex < units.length - 1) {
    size /= 1024
    unitIndex++
  }
  return `${size.toFixed(1)} ${units[unitIndex]}`
}

const getSubmissionPercentage = () => {
  if (!props.assignment || !props.assignment.submission_stats) return 0
  const { submitted, total } = props.assignment.submission_stats
  return Math.round((submitted / total) * 100)
}

const format = (percentage) => {
  if (!props.assignment || !props.assignment.submission_stats) return '0/0'
  const { submitted, total } = props.assignment.submission_stats
  return `${submitted}/${total}`
}

const getStatusClass = () => {
  if (!props.assignment || !props.assignment.due_date) return 'status-normal'
  const now = new Date()
  const dueDate = new Date(props.assignment.due_date)
  if (now > dueDate) return 'status-expired'
  if (now > new Date(dueDate - 24 * 60 * 60 * 1000)) return 'status-urgent'
  return 'status-normal'
}

const getStatusText = () => {
  if (!props.assignment || !props.assignment.due_date) return '进行中'
  const now = new Date()
  const dueDate = new Date(props.assignment.due_date)
  if (now > dueDate) return '已截止'
  if (now > new Date(dueDate - 24 * 60 * 60 * 1000)) return '即将截止'
  return '进行中'
}

const handleClose = () => {
  emit('close')
}
</script>

<style scoped>
.detail-dialog :deep(.el-dialog__body) {
  padding: 0;
}

.assignment-detail {
  padding: 24px;
}

.header-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header-section h2 {
  margin: 0;
  color: #303133;
  font-size: 24px;
}

.status-badge {
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
}

.status-normal {
  background-color: #e1f3d8;
  color: #67c23a;
}

.status-urgent {
  background-color: #fdf6ec;
  color: #e6a23c;
}

.status-expired {
  background-color: #fef0f0;
  color: #f56c6c;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
  margin-bottom: 32px;
}

.info-item {
  padding: 16px;
  background-color: #f8f9fa;
  border-radius: 8px;
}

.info-label {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #606266;
  margin-bottom: 8px;
}

.info-value {
  color: #303133;
  font-size: 16px;
  font-weight: 500;
}

.info-value.highlight {
  color: #409eff;
  font-size: 20px;
}

.description-section {
  margin-bottom: 32px;
}

.description-section h3,
.attachments-section h3 {
  color: #303133;
  margin-bottom: 16px;
  font-size: 18px;
}

.description-content {
  white-space: pre-wrap;
  line-height: 1.6;
  color: #606266;
  background-color: #f8f9fa;
  padding: 16px;
  border-radius: 8px;
}

.attachment-list {
  background-color: #f8f9fa;
  border: none;
}

.attachment-item {
  padding: 12px;
  display: flex;
  align-items: center;
  transition: background-color 0.3s;
}

.attachment-item:hover {
  background-color: #ecf5ff;
}

.attachment-item:not(:last-child) {
  border-bottom: 1px solid #ebeef5;
}

.attachment-item .el-icon {
  margin-right: 8px;
  font-size: 16px;
}

.file-size {
  margin-left: 8px;
  color: #909399;
  font-size: 12px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  padding: 16px 24px;
  background-color: #f8f9fa;
  border-top: 1px solid #dcdfe6;
}
.submission-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.submission-count {
  white-space: nowrap;
  color: #606266;
}

/* 调整进度条宽度 */
.submission-info :deep(.el-progress) {
  flex: 1;
  margin-right: 8px;
}
</style>